package com.hap.bank.controllers;

import com.hand.hap.core.IRequest;
import com.hand.hap.system.controllers.BaseController;
import com.hand.hap.system.dto.ResponseData;
import com.hap.bank.dto.HandAccount;
import com.hap.bank.dto.HandAccountInfo;
import com.hap.bank.service.IHandAccountService;
import com.hap.bank.service.IHandEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: FizzyElf
 * Date: 2022/1/2 21:37
 * Description:
 */
@Controller
public class HandAccountInfoController extends BaseController {
    @Autowired
    private IHandAccountService service;

    @Autowired
    private IHandEmployeeService iHandEmployeeService;


    @RequestMapping(value = "/test/accountInfo/query")
    @ResponseBody
    public ResponseData query(HandAccountInfo handAccountInfo,
                              @RequestParam(defaultValue = DEFAULT_PAGE) int page,
                              @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) int pageSize){
        System.out.println(handAccountInfo);
        return new ResponseData(service.selectAccountInfo(handAccountInfo,pageSize,page));
    }

    @RequestMapping(value = "/test/accountInfo/submit")
    @ResponseBody
    public ResponseData update(@RequestBody List<HandAccountInfo> dto, BindingResult result, HttpServletRequest request) {
        System.out.println(dto);
        getValidator().validate(dto, result);
        if (result.hasErrors()) {
            ResponseData responseData = new ResponseData(false);
            responseData.setMessage(getErrorMessage(result, request));
            return responseData;
        }
        IRequest requestCtx = createRequestContext(request);
        List<HandAccount> accountList = new ArrayList<>();
        for(HandAccountInfo handAccountInfo : dto){
            HandAccount account = new HandAccount();
            account.setAccountId(handAccountInfo.getAccountId());
            account.setEmployeeId(iHandEmployeeService.selectIdByName(handAccountInfo.getEmpName()));
            account.setBankAccountNum(handAccountInfo.getBankAccountNum());
            account.setBankAccountName(handAccountInfo.getBankAccountName());
            account.setEnabledFlag(handAccountInfo.getEnabledFlag());
            account.setBankNum(handAccountInfo.getBankNum());
            account.setCurrencyCode(handAccountInfo.getCurrencyCode());
            account.setPrimaryFlag(handAccountInfo.getPrimaryFlag());
            account.setAmount(handAccountInfo.getAmount());
            account.setAccountDate(handAccountInfo.getAccountDate());
            account.set__status(handAccountInfo.get__status());
            accountList.add(account);
        }

        return new ResponseData(service.batchUpdate(requestCtx, accountList));
    }
}